//
//  XMUserDetailAboutTableViewCell.m
//  XunMi
//
//  Created by MacBook on 2024/2/20.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMUserDetailAboutTableViewCell.h"
#import "XMCollectionFlowLayout.h"

@interface XMUserDetailAboutTableViewCell () <UICollectionViewDelegate, UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UIImageView *realImgV;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UIButton *moreButton;
@property (weak, nonatomic) IBOutlet UILabel *idLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSpace;
@property (weak, nonatomic) IBOutlet UILabel *nameLa;
@property (weak, nonatomic) IBOutlet UILabel *aboutLa;
@property (weak, nonatomic) IBOutlet UIView *dtView;

@property (weak, nonatomic) IBOutlet UIView *sexBg;
@property (weak, nonatomic) IBOutlet UILabel *ageLa;
@property (weak, nonatomic) IBOutlet UIImageView *sexImgV;
@property (nonatomic, strong) NSArray *imgsArray;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *collectionW;

@end

@implementation XMUserDetailAboutTableViewCell

- (void)setModel:(XMUserModel *)model {
    _model = model;
    self.realImgV.image = kGetImage(model.idcard_verify ? @"user_d_real_y" : @"user_d_real_n");
    self.nameLa.hidden = model.alias.length == 0 ? YES : NO;
    self.topSpace.constant = model.alias.length == 0 ? 0 : 15;
    self.nameLa.text = kStringFormat(@"昵称：%@", model.nickname);
    self.nameLa.textColor = model.showColor;
    self.dtView.hidden = model.dynamic_list.count == 0;
    self.aboutLa.text = model.isMan ? @"关于他:" : @"关于她:";
    
    self.ageLa.text = kStringFormat(@"%@",model.age);
    if (model.isMan) {
        self.sexBg.backgroundColor = kHEXA(@"#3B8CFF", 0.2);
        self.ageLa.textColor = kHEX(@"#3B8CFF");
        self.sexImgV.image = kGetImage(@"user_d_m");
    } else {
        self.sexBg.backgroundColor = kHEXA(@"#E74E63", 0.2);
        self.ageLa.textColor = kHEX(@"#E74E63");
        self.sexImgV.image = kGetImage(@"user_d_wm");
    }
    
    self.idLabel.text = kStringFormat(@"(ID：%@)", model.person_id);
    
    if (self.model.dynamic_list.count >= 4) {
        self.imgsArray = [self.model.dynamic_list subarrayWithRange:NSMakeRange(0, 4)];
    } else {
        self.imgsArray = [NSArray arrayWithArray:self.model.dynamic_list];
    }
    
    if (self.model.dynamic_list.count > 0) {
        self.moreButton.hidden = NO;
        self.collectionW.constant = self.imgsArray.count * 36 + (self.imgsArray.count - 1) * 10;
        [self.collectionView reloadData];
    } else {
        self.moreButton.hidden = YES;
    }
    
    NSLog(@"imgsArray:%@", self.imgsArray);
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(36, 36);
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    [self.collectionView setCollectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:XMUserDetailDTCollectionCell.class forCellWithReuseIdentifier:kStringFromClass(XMUserDetailDTCollectionCell)];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [_moreButton setImagePosition:(XMImagePositionRight) spacing:8];
}

- (IBAction)moreAction:(UIButton *)sender {
    if (self.moreBlock) {
        self.moreBlock(self.model);
    }
}

#pragma mark - UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.imgsArray.count == 0) {
        return 0;
    }
    return self.imgsArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.imgsArray.count == 0) {
        return nil;
    }
    XMUserDetailDTCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"XMUserDetailDTCollectionCell" forIndexPath:indexPath];
    [cell.imgV sd_setImageWithURL:kStringToUrl(self.imgsArray[indexPath.item]) placeholderImage:kDefaultCoverImg];
    return cell;
}

@end



@implementation XMUserDetailDTCollectionCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self == [super initWithFrame:frame]) {
        self.imgV = [[UIImageView alloc] initWithImage:kDefaultCoverImg];
        self.imgV.contentMode = UIViewContentModeScaleAspectFill;
        [self.contentView addSubview:self.imgV];
        [self.imgV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.contentView);
        }];
        kCornerRadius(_imgV, 6, 0, UIColor.clearColor);
    }
    return self;
}

@end
